import { fetchList } from "../..";
import {
  analysisUsers,
  defaultModalFormState,
  defaultModalLoading,
  defaultModalVisible,
} from "../../store";

export default () => {
  const disabled = computed(() => {
    const formState = defaultModalFormState[3];
    return !formState.targetId;
  });
  const defineModalConfig = {
    disable_ok_button: disabled,
    modalInstance: ref(null),
    loading: defaultModalLoading[3],
    visible: defaultModalVisible[3],
    maskClosable: true,
    title: ref($t("base.customDispense")),
    reverse: ref(true),
    formState: defaultModalFormState[3],
    on: {
      async finish(values: any) {
        const formState = defaultModalFormState[3];
        const loading = defaultModalLoading[3];
        try {
          loading.value = true;
          await userApi.updateAssign({
            userId: formState.userId,
            targetId: formState.targetId,
          });
          message.success($t("base.dispenseSuccess"));
          fetchList();
          loading.value = false;
          defaultModalVisible[3].value = false;
        } catch (error: any) {
          loading.value = false;
          message.error(error.message);
        }
      },
    },
    modalList: [
      {
        label: getProxyT("table102"),
        name: "originUser",
        disabled: true,
        type: "input",
        model: "originUser",
        placeholder: getProxyT("placeholder54"),
      },
      {
        label: getProxyT("table103"),
        name: "targetId",
        options: analysisUsers,
        type: "select",
        model: "targetId",
        placeholder: getProxyT("placeholder54"),
        allowClear: true,
        help: (
          <>
            <i class="fs10">{$t("placeholder55")}</i>
          </>
        ),
        rules: [
          {
            required: true,
            validator(rule: any, value: any) {
              if (!value) {
                defineModalConfig.modalList[1].help = (
                  <>
                    <i class="fs10">{$t("placeholder54")}</i>
                  </>
                );
                return Promise.reject();
              }
              defineModalConfig.modalList[1].help = (
                <>
                  <i class="fs10">{$t("placeholder55")}</i>
                </>
              );
              return Promise.resolve();
            },
            trigger: ["change", "blur"],
          },
        ],
      },
    ],
  };
  return defineModalConfig;
};
